package com.aix.demo.activity;

import java.util.Timer;
import java.util.TimerTask;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.aix.android.framework.activity.BaseActivity;
import com.aix.demo.R;

public class TimerTaskWithoutServiceActivity extends BaseActivity {
	
	private Timer timer;
	
	private TimerTask timerTask;
	
	private Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				showToast(1111, Toast.LENGTH_SHORT);
			}
		}
		
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_timer_task_without_service);
		
		Button btn = (Button) findViewById(R.id.btn1);
		btn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				timer = new Timer();
				timerTask = new TimerTask() {
					
					@Override
					public void run() {
						Message msg = Message.obtain(mHandler, 1);
						mHandler.sendMessage(msg);
					}
				};
				timer.schedule(timerTask, 0, 5000);
			}
		});
		
		btn = (Button) findViewById(R.id.btn2);
		btn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				timer.cancel();
				timerTask.cancel();
			}
		});
	}
}
